package com.example.controller;

//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Student;
import com.example.mapper.StudentMapper;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Student")
public class StudentConstoller {
    @Autowired
    private StudentService studentService;

    @Autowired
    private StudentMapper studentMapper;
    //修改和更新数据
    @PostMapping
    public Map<String, Object> saveStudent(@RequestBody Student student) {
        return studentService.saveStudent(student);
    }

    //展示所有数据
    @GetMapping
    public List<Student> index() {
        List<Student> all = studentMapper.finAll();
        return all;
    }

//根据学号删除数据
@DeleteMapping("/stu_num")
public Map<String, Object> deleteByStuNum(@RequestParam String stu_num) {
    return studentService.deleteByStuNum(stu_num);
}





//    @GetMapping("/page")
//    public IPage<Student> findPage(@RequestParam Integer pageNum,
//                                @RequestParam Integer pageSize,
//                                @RequestParam(defaultValue = "") String name,
//                                @RequestParam(defaultValue = "") String faculty,
//                                @RequestParam(defaultValue = "") String classs) {
//        IPage<Student> page = new Page<>(pageNum, pageSize);
//        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
//        if (!"".equals(name)) {
//            queryWrapper.like("name", name);
//        }
//        if (!"".equals(faculty)) {
//            queryWrapper.like("faculty", faculty);
//        }
//        if (!"".equals(classs)) {
//            queryWrapper.like("classs", classs);
//        }
//        return studentService.page(page, queryWrapper);




    // 分页查询
    //  接口路径：/user/page?pageNum=1&pageSize=10
    // @RequestParam接受
//    limit第一个参数 = (pageNum - 1) * pageSize
    // pageSize
    @GetMapping("/page")
    public  Map<String, Object> findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                         @RequestParam String name) {
        pageNum = (pageNum - 1) * pageSize;
        List<Student> data = studentMapper.selectPage(pageNum,pageSize,name);
        Integer total = studentMapper.selectTotal(name);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }
}
